package com.yao.config;

import com.yao.constant.HttpConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/5 22:45
 * FileName: FeignConfig
 * Description:
 */

@Configuration
public class FeignConfig {


        @Bean
        public RequestInterceptor requestInterceptor(){
            return new RequestInterceptor() {
                @Override
                public void apply(RequestTemplate requestTemplate) {
                    ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                    if(requestAttributes != null){
                        //获取请求的token信息
                        String token = requestAttributes.getRequest().getHeader(HttpConstant.AUTH_HEADER);
                        //同步到请求中
                        requestTemplate.header(HttpConstant.AUTH_HEADER,token);
                        return;
                    }
                }
            };
        }

}
